﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Emerson.Storage.Service.Interface;
using Emerson.Storage.Entities;
using Emerson.Storage.Agent.Core;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;
using System.ServiceModel;
using System.Net;

namespace Emerson.Storage.Service
{
    /// <summary>
    /// The ApplicationService inherites all the WCF service contracts and implements all the business operations.
    /// However, it brokers some operations to other implementation classes as WorkstationServce, UserServie etc.
    /// </summary>
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public sealed class ApplicationService : IApplicationService, IWorkstationService, IUserService, ISettingsService
    {
        WorkstationService wsService;
        SettingsService settingsService;
        UpdateManager updateManager;

        public ApplicationService()
        {
            TcpClientChannel chan = new TcpClientChannel();
            ChannelServices.RegisterChannel(chan, false);

            wsService = new WorkstationService();
            settingsService = new SettingsService();
            updateManager = new UpdateManager();

            updateManager.StartUpdating();


        }

        #region IApplicationService Members

        ApplicationInfo IApplicationService.GetApplicationInfo()
        {
            ApplicationInfo ai = new ApplicationInfo();
            using (SmartStorageEntities sse = new SmartStorageEntities())
            {
                ai.DatabaseName = sse.DefaultContainerName;
            }

            ai.ServerName = Environment.MachineName;
            IPAddress[] addresses = Dns.GetHostAddresses(ai.ServerName);
            for (int i = 0; i < addresses.Length; i++)
            {
                if (addresses[i].AddressFamily.ToString().Equals("InterNetwork"))
                {
                    ai.ServerAddress = addresses[i].ToString();
                    break;
                }
            }


            return ai;
        }

        void IApplicationService.Start()
        {
            throw new NotImplementedException();
        }

        void IApplicationService.Stop()
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IWorkstationService Members

        public WorkstationRoot GetWorkstationRoot()
        {
            return wsService.GetWorkstationRoot();
        }

        public Workstation GetWorkstation(int workstationId)
        {
            return wsService.GetWorkstation(workstationId);
        }

        public void AddWorkstation(int groupId, Workstation newWorkstation)
        {
            wsService.AddWorkstation(groupId, newWorkstation);
        }

        public void RemoveWorkstation(int workstationId)
        {
            throw new NotImplementedException();
        }

        public void AddGroup(Group newGroup)
        {
            wsService.AddGroup(newGroup);
        }

        public void DeleteGroup(int groupId)
        {
            wsService.DeleteGroup(groupId);
        }

        public void DeleteWorkstation(int workstationid)
        {
            wsService.DeleteWorkstation(workstationid);
        }

        public Workstation FindWorkstation(string address)
        {
            return wsService.FindWorkstation(address);
        }

        public Battery GetBattery(int deviceId)
        {
            return wsService.GetBattery(deviceId);
        }

        public void DeleteBattery(int deviceId)
        {
            wsService.DeleteBattery(deviceId);
        }

        public Group GetGroup(int groupId)
        {
            return wsService.GetGroup(groupId);
        }
        #endregion
    }
}
